﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.Composite.Extensions;
using Microsoft.Practices.Composite.Regions;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.Help.Interface;
using System.Reflection;
using Jbe.TestSuite.Help.Properties;

namespace Jbe.TestSuite.Help.Applications
{
    internal class ModuleController
    {
        private readonly IApplicationContainer container;
        private readonly IRegionManager regionManager;
        private readonly IUIElementCreationService uiElementCreationService;
        private IDynamicHelp dynamicHelp;
        private HelpPresentationModel presentationModel;


        public ModuleController(IApplicationContainer container, IRegionManager regionManager, IUIElementCreationService uiElementCreationService)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.uiElementCreationService = uiElementCreationService;
        }


        public void Initialize()
        {
            dynamicHelp = container.Resolve<IDynamicHelp>();

            IMenuItem menuItem = uiElementCreationService.CreateMenuItem(Resources.ContentsMenu, Resources.Help.ToBitmap());
            menuItem.Click += (sender, e) => ShowContents();
            regionManager.Regions[ShellRegionNames.HelpMenuItems].Add(menuItem);

            menuItem = uiElementCreationService.CreateMenuItem(Resources.CreditsMenu);
            menuItem.Click += (sender, e) => ShowCredits();
            regionManager.Regions[ShellRegionNames.HelpMenuItems].Add(menuItem);

            menuItem = uiElementCreationService.CreateMenuItem(Resources.LicenseMenu);
            menuItem.Click += (sender, e) => ShowLicense();
            regionManager.Regions[ShellRegionNames.HelpMenuItems].Add(menuItem);

            ShowContents();
        }

        private void ShowHelpView()
        {
            if (presentationModel == null)
            {
                presentationModel = container.Resolve<HelpPresentationModel>();
                presentationModel.View.DataContext = presentationModel;
                regionManager.Regions[ShellRegionNames.RightRegion].Add(presentationModel.View);
            }
            regionManager.Regions[ShellRegionNames.RightRegion].Activate(presentationModel.View);
        }

        private void ShowContents()
        {
            ShowHelpView();
            dynamicHelp.ShowHelp(Assembly.GetExecutingAssembly(), "Resources/Help/Contents.htm");
        }

        private void ShowCredits()
        {
            ShowHelpView();
            dynamicHelp.ShowHelp(Assembly.GetExecutingAssembly(), "Resources/Help/Credits.htm");
        }

        private void ShowLicense()
        {
            ShowHelpView();
            dynamicHelp.ShowHelp(Assembly.GetExecutingAssembly(), "Resources/Help/License.htm");
        }
    }
}
